import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard'}
    }]
  }
]

export const asyncRoutes = [
  {
    path: '/article',
    component: Layout,
    redirect: '/article',
    name: 'Article',
    alwaysShow: true,
    meta: { title: '文章管理', icon: 'el-icon-setting' },
    children: [
      {
        path: 'column',
        name: 'Column',
        component: () => import('@/views/article/column'),
        meta: { title: '文章分类', icon: 'el-icon-s-unfold' }
      },
      {
        path: 'list',
        name: 'List',
        component: () => import('@/views/article/list'),
        meta: { title: '文章列表', icon: 'el-icon-tickets' }
      },
      {
        path: 'enroll',
        name: 'Enroll',
        component: () => import('@/views/article/enroll'),
        meta: { title: '报名列表', icon: 'el-icon-edit-outline' }
      }
    ]
  },
  {
    path: '/platform',
    component: Layout,
    redirect: '/platform',
    name: 'Platform',
    alwaysShow: true,
    meta: { title: '网站设置', icon: 'el-icon-setting' },
    children: [
      {
        path: 'setting',
        name: 'Setting',
        component: () => import('@/views/platform/setting'),
        meta: { title: '系统配置', icon: 'el-icon-s-tools' }
      },
      {
        path: 'nous',
        name: 'Nous',
        component: () => import('@/views/platform/nous'),
        meta: { title: '旅游常识', icon: 'el-icon-document' }
      },
      {
        path: 'problem',
        name: 'Problem',
        component: () => import('@/views/platform/problem'),
        meta: { title: '常见问题', icon: 'el-icon-document' }
      },
      {
        path: 'about',
        name: 'About',
        component: () => import('@/views/platform/about'),
        meta: { title: '关于我们', icon: 'el-icon-document' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
